/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.dubbo.config.spring.util;

import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.annotation.Service;

import org.springframework.core.env.Environment;
import org.springframework.core.env.PropertyResolver;
import org.springframework.util.StringUtils;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static java.lang.String.valueOf;
import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
import static org.springframework.core.annotation.AnnotationUtils.getAnnotationAttributes;
import static org.springframework.core.annotation.AnnotationUtils.getDefaultValue;
import static org.springframework.util.CollectionUtils.arrayToList;
import static org.springframework.util.ObjectUtils.nullSafeEquals;
import static org.springframework.util.StringUtils.trimWhitespace;

/**
 * Annotation Utilities Class
 *
 * @see org.springframework.core.annotation.AnnotationUtils
 * @since 2.5.11
 */
public class AnnotationUtils {

  public static String resolveInterfaceName(Service service, Class<?> defaultInterfaceClass)
      throws IllegalStateException {

    String interfaceName;
    if (StringUtils.hasText(service.interfaceName())) {
      interfaceName = service.interfaceName();
    } else if (!void.class.equals(service.interfaceClass())) {
      interfaceName = service.interfaceClass().getName();
    } else if (defaultInterfaceClass.isInterface()) {
      interfaceName = defaultInterfaceClass.getName();
    } else {
      throw new IllegalStateException(
          "The @Service undefined interfaceClass or interfaceName, and the type "
              + defaultInterfaceClass.getName() + " is not a interface.");
    }

    return interfaceName;

  }

  public static String resolveInterfaceName(Reference reference, Class<?> defaultInterfaceClass)
      throws IllegalStateException {

    String interfaceName;
    if (!"".equals(reference.interfaceName())) {
      interfaceName = reference.interfaceName();
    } else if (!void.class.equals(reference.interfaceClass())) {
      interfaceName = reference.interfaceClass().getName();
    } else if (defaultInterfaceClass.isInterface()) {
      interfaceName = defaultInterfaceClass.getName();
    } else {
      throw new IllegalStateException(
          "The @Reference undefined interfaceClass or interfaceName, and the type "
              + defaultInterfaceClass.getName() + " is not a interface.");
    }

    return interfaceName;

  }

  // Cloned from https://github.com/alibaba/spring-context-support/blob/1.0.2/src/main/java/com/alibaba/spring/util/AnnotationUtils.java

  /**
   * Is specified {@link Annotation} present on {@link Method}'s declaring class or parameters or
   * itself.
   *
   * @param method {@link Method}
   * @param annotationClass {@link Annotation} type
   * @param <A> {@link Annotation} type
   * @return If present , return <code>true</code> , or <code>false</code>
   * @since 2.6.6
   */
  public static <A extends Annotation> boolean isPresent(Method method, Class<A> annotationClass) {

    Map<ElementType, List<A>> annotationsMap = findAnnotations(method, annotationClass);

    return !annotationsMap.isEmpty();

  }

  /**
   * Find specified {@link Annotation} type maps from {@link Method}
   *
   * @param method {@link Method}
   * @param annotationClass {@link Annotation} type
   * @param <A> {@link Annotation} type
   * @return {@link Annotation} type maps , the {@link ElementType} as key , the list of {@link
   * Annotation} as value. If {@link Annotation} was annotated on {@link Method}'s parameters{@link
   * ElementType#PARAMETER} , the associated {@link Annotation} list may contain multiple elements.
   * @since 2.6.6
   */
  public static <A extends Annotation> Map<ElementType, List<A>> findAnnotations(Method method,
      Class<A> annotationClass) {

    Retention retention = annotationClass.getAnnotation(Retention.class);

    RetentionPolicy retentionPolicy = retention.value();

    if (!RetentionPolicy.RUNTIME.equals(retentionPolicy)) {
      return Collections.emptyMap();
    }

    Map<ElementType, List<A>> annotationsMap = new LinkedHashMap<ElementType, List<A>>();

    Target target = annotationClass.getAnnotation(Target.class);

    ElementType[] elementTypes = target.value();

    for (ElementType elementType : elementTypes) {

      List<A> annotationsList = new LinkedList<A>();

      switch (elementType) {

        case PARAMETER:

          Annotation[][] parameterAnnotations = method.getParameterAnnotations();

          for (Annotation[] annotations : parameterAnnotations) {

            for (Annotation annotation : annotations) {

              if (annotationClass.equals(annotation.annotationType())) {

                annotationsList.add((A) annotation);

              }

            }

          }

          break;

        case METHOD:

          A annotation = findAnnotation(method, annotationClass);

          if (annotation != null) {

            annotationsList.add(annotation);

          }

          break;

        case TYPE:

          Class<?> beanType = method.getDeclaringClass();

          A annotation2 = findAnnotation(beanType, annotationClass);

          if (annotation2 != null) {

            annotationsList.add(annotation2);

          }

          break;

      }

      if (!annotationsList.isEmpty()) {

        annotationsMap.put(elementType, annotationsList);

      }


    }

    return Collections.unmodifiableMap(annotationsMap);

  }

  /**
   * Get the {@link Annotation} attributes
   *
   * @param annotation specified {@link Annotation}
   * @param ignoreDefaultValue whether ignore default value or not
   * @param ignoreAttributeNames the attribute names of annotation should be ignored
   * @return non-null
   * @since 2.6.6
   */
  public static Map<String, Object> getAttributes(Annotation annotation, boolean ignoreDefaultValue,
      String... ignoreAttributeNames) {
    return getAttributes(annotation, null, ignoreDefaultValue, ignoreAttributeNames);
  }

  /**
   * Get the {@link Annotation} attributes
   *
   * @param annotation specified {@link Annotation}
   * @param propertyResolver {@link PropertyResolver} instance, e.g {@link Environment}
   * @param ignoreDefaultValue whether ignore default value or not
   * @param ignoreAttributeNames the attribute names of annotation should be ignored
   * @return non-null
   * @since 2.6.6
   */
  public static Map<String, Object> getAttributes(Annotation annotation,
      PropertyResolver propertyResolver,
      boolean ignoreDefaultValue, String... ignoreAttributeNames) {

    Set<String> ignoreAttributeNamesSet = new HashSet<String>(arrayToList(ignoreAttributeNames));

    Map<String, Object> attributes = getAnnotationAttributes(annotation);

    Map<String, Object> actualAttributes = new LinkedHashMap<String, Object>();

    for (Map.Entry<String, Object> entry : attributes.entrySet()) {

      String attributeName = entry.getKey();
      Object attributeValue = entry.getValue();

      // ignore default attribute value
      if (ignoreDefaultValue && nullSafeEquals(attributeValue,
          getDefaultValue(annotation, attributeName))) {
        continue;
      }

      // ignore attribute name
      if (ignoreAttributeNamesSet.contains(attributeName)) {
        continue;
      }

      /**
       * @since 2.7.1
       * ignore annotation member
       */
      if (attributeValue.getClass().isAnnotation()) {
        continue;
      }
      if (attributeValue.getClass().isArray() && attributeValue.getClass().getComponentType()
          .isAnnotation()) {
        continue;
      }

      if (attributeValue instanceof String) {
        attributeValue = resolvePlaceholders(valueOf(attributeValue), propertyResolver);
      } else if (attributeValue instanceof String[]) {
        String[] values = (String[]) attributeValue;
        for (int i = 0; i < values.length; i++) {
          values[i] = resolvePlaceholders(values[i], propertyResolver);
        }
        attributeValue = values;
      }
      actualAttributes.put(attributeName, attributeValue);
    }
    return actualAttributes;
  }

  private static String resolvePlaceholders(String attributeValue,
      PropertyResolver propertyResolver) {
    String resolvedValue = attributeValue;
    if (propertyResolver != null) {
      resolvedValue = propertyResolver.resolvePlaceholders(resolvedValue);
      resolvedValue = trimWhitespace(resolvedValue);
    }
    return resolvedValue;
  }

}
